<?php
class PersonalApp extends ManagerApp {
   
   	//学生信息栏目界面
    public function studentAct(){
    	$userid = My::arr2var($_SESSION,'member.memberid');
        $search['type'] = My::arr2var($_SESSION,'member.type');
        $search['time'] = isset($_COOKIE['time_personal']) ? $_COOKIE['time_personal'] : '';
        $result = My::getCache($search,array('pre'=>'personal'));
        if($result == ''){
            $result = DB('homeuser')->clear()->where("id={$userid}")->find();
            $result['college'] = M('admin.category')->item2submenu('college',array('level'=>'1'));
            $result['params'] = My::decodeJson($result['params']);
            $result['log'] = My::decodeJson($result['log']);
            My::setCache($search,$result,array('pre'=>'personal'));
        }
        if(empty($result['params']['partner'])){
            $result['params']['partner'] = array();
            array_push($result['params']['partner'],array('partner_name'=>'','partner_sex'=>'','partner_phone'=>'','partner_school'=>'','partner_xy'=>'','partner_class'=>''));
        }
        if(empty($result['params']['award'])){
            $result['params']['award'] = array();
            array_push($result['params']['award'],array('work_date5'=>'','complete_type'=>'','reward_type'=>'','reward_money'=>''));
        }
    	$this->assign('result',$result);
		$this->display();
    }

    //老师信息栏目界面
    public function teacherAct(){
        $userid = My::arr2var($_SESSION,'member.memberid');
        $search['type'] = My::arr2var($_SESSION,'member.type');
        $search['time'] = isset($_COOKIE['time_personal_t']) ? $_COOKIE['time_personal_t'] : '';
        $result = My::getCache($search,array('pre'=>'personal'));
        if($result==''){
            $result = DB('homeuser')->clear()->where("id={$userid}")->find();
            $result['college'] = M('admin.category')->item2submenu('college',array('level'=>'1'));
            $result['params'] = My::decodeJson($result['params']);
            $result['params_tutor'] = My::decodeJson($result['params_tutor']);
            $result['log'] = My::decodeJson($result['log']);
            My::setCache($search,$result,array('pre'=>'personal'));
        }
        $this->assign('result',$result);
        $this->display();
    }

    //个人信息修改验证
    public function infoAct(){
    	$params = My::arr2var($_POST,'params');
        $_POST['editdate'] = $date = date("Y-m-d H:i:s");//当前时间
        $now = strtotime($date);
    	if(!My::is_string($_POST['username'],array('c')))
    		$this->ajaxReturn(300,'姓名填写有误！');
        if(!My::is_string($params['photo']))
            $this->ajaxReturn(300,'请选择头像');
        $_POST['photo'] = $_POST['params']['photo'];
        unset($_POST['params']['photo']);
        if(!My::is_date($params['student']['work_date0']) || (strtotime($params['student']['work_date0']) >= $now))
            $this->ajaxReturn(300,'出生日期填写有误！');
        $_POST['params']['birthdate'] = $params['student']['work_date0'];
        unset($_POST['params']['student']['work_date0']);
        if(!My::is_string($params['nation'],array('c')))
            $this->ajaxReturn(300,'民族填写有误！');
    	if(!My::is_card($params['indentify']))
   			$this->ajaxReturn(300,'身份证填写有误！');
        if(!My::is_id($_POST['college_id']))
            $this->ajaxReturn(300,'请选择二级学院！');
        if(empty($params['class']))
            $this->ajaxReturn(300,'班级填写有误！');
    	if(!My::is_id($params['number']))
    		$this->ajaxReturn(300,'学号填写有误！');
    	if(!My::is_tel($_POST['mobile']))
    		$this->ajaxReturn(300,'手机号码填写有误！');
    	if(!My::is_email($_POST['email']))
    		$this->ajaxReturn(300,'邮箱填写有误！');
        if(!My::is_string($params['qq']))
            $this->ajaxReturn(300,'QQ填写有误！');
        if(!My::is_string($params['address']))
            $this->ajaxReturn(300,'联系地址及邮编填写有误！');
        if(!empty($params['company_work']['work_date1']) && strtotime($params['company_work']['work_date1']) > $now)
            $this->ajaxReturn(300,'创业团队工作开始时间不能大于当前时间！');
        if(!empty($params['company_work']['work_date1']) && empty($params['company_work']['work_date2']))
            $this->ajaxReturn(300,'请选择截止时间');
        if(!empty($params['company_work']['work_date1']) && strtotime($params['company_work']['work_date1']) > strtotime($params['company_work']['work_date2']) && !empty($params['company_work']['work_date2']))
            $this->ajaxReturn(300,'开始时间不能大于截止时间！');
        if(!empty($params['company_work']['name']) && !My::is_string($params['company_work']['name']))
            $this->ajaxReturn(300,'公司名称输入有误！');
        if(!empty($params['company_work']['registration']) && !My::is_id($params['company_work']['registration']))
            $this->ajaxReturn(300,'注册号输入有误！');
        if(!empty($params['company_work']['money']) && !My::is_id($params['company_work']['money']))
            $this->ajaxReturn(300,'注册资金输入有误！');
        if(!empty($params['company_work']['address']) && !My::is_string($params['company_work']['address']))
            $this->ajaxReturn(300,'公司所在地输入有误！');
        if(!empty($params['company_work']['code']) && !My::is_string($params['company_work']['code']))
            $this->ajaxReturn(300,'公司地址及邮编输入有误！');
        if(!empty($params['company_work']['type']) && !My::is_string($params['company_work']['type']))
            $this->ajaxReturn(300,'公司类型及邮编输入有误！');
        if(!empty($params['company_work']['range']) && !My::is_string($params['company_work']['range']))
            $this->ajaxReturn(300,'经营范围及邮编输入有误！');
        if(!empty($params['company_work']['size']) && !My::is_id($params['company_work']['size']))
            $this->ajaxReturn(300,'公司面积输入有误！');
        if(!empty($params['company_work']['teacher']) && !My::is_string($params['company_work']['teacher']))
            $this->ajaxReturn(300,'指导老师输入有误！');
        if(!empty($params['company_work']['brief']) && !My::is_string($params['company_work']['brief']))
            $this->ajaxReturn(300,'公司简介输入有误！');
        if(!empty($params['company_work']['number']) && !My::is_id($params['company_work']['number']))
            $this->ajaxReturn(300,'合伙人数输入有误！');
        if(!empty($params['company_work']['member']) && !My::is_id($params['company_work']['member']))
            $this->ajaxReturn(300,'员工人数输入有误！');
        
        //处理合伙人数据
        if(isset($_POST['params']['student'])){
            foreach($_POST['params']['student'] as $key => $value){
                foreach($value as $keys => $values){
                   $_POST['params']['partner'][$keys][$key] = $values;
                   unset($_POST['params']['student']);
                }
            }
            //除去没有填的内容
            foreach($_POST['params']['partner'] as $key => $value){
                if(empty($value['partner_name']) && empty($value['partner_phone']) && empty($value['partner_school']) && empty($value['partner_xy']) && empty($value['partner_class']))
                    unset($_POST['params']['partner'][$key]);
            }
            //对处理后的合伙人数据进行判断
            foreach($_POST['params']['partner'] as $key => $value){
                ++$key;
                if(!My::is_string($value['partner_name'],array('c')))
                    $this->ajaxReturn(300,'合伙人第'."$key".'个的结姓名填写有误！');
                if(!in_array($value['partner_sex'], array('男','女')))
                    $this->ajaxReturn(300,'合伙人第'."$key".'个的性别填写有误！');
                if(!My::is_tel($value['partner_phone']))
                    $this->ajaxReturn(300,'合伙人第'."$key".'个的电话填写有误！');
                if(!My::is_string($value['partner_school']))
                    $this->ajaxReturn(300,'合伙人第'."$key".'个的学校填写有误！');
                if(!My::is_string($value['partner_xy']))
                    $this->ajaxReturn(300,'合伙人第'."$key".'个的院系填写有误！');
                if(!My::is_string($value['partner_class']))
                    $this->ajaxReturn(300,'合伙人第'."$key".'个的班级填写有误！');
            }
        }
        //处理获奖情况
        if(isset($_POST['params']['award1'])){
            foreach($_POST['params']['award1'] as $key => $value){
                foreach($value as $keys => $values){
                   $_POST['params']['award'][$keys][$key] = $values;
                   unset($_POST['params']['award1']);
                }
            }
            foreach($_POST['params']['award'] as $key => $value){
                if(empty($value['work_date5']) && empty($value['complete_type']) && empty($value['reward_type']) && empty($value['reward_money']))
                    unset($_POST['params']['award'][$key]);
            }
            foreach($_POST['params']['award'] as $key => $value){
                ++$key;
                if(!My::is_date($value['work_date5']))
                    $this->ajaxReturn(300,'获奖情况第'."$key".'个的时间填写有误！');
                if(strtotime($value['work_date5']) > $now)
                    $this->ajaxReturn(300,'获奖情况第'."$key".'个的时间不能大于当前时间！');
                if(!My::is_string($value['complete_type']))
                    $this->ajaxReturn(300,'获奖情况第'."$key".'个的竞赛类型填写有误！');
                if(!My::is_string($value['reward_type']))
                    $this->ajaxReturn(300,'获奖情况第'."$key".'个的获奖级别填写有误！');
                if(!My::is_id($value['reward_money']))
                    $this->ajaxReturn(300,'获奖情况第'."$key".'个的获奖金额填写有误！');
            }
        }
    	$userid = My::arr2var($_SESSION,'member.memberid');//用户id
        $search['type'] = My::arr2var($_SESSION,'member.type');
        $search['time'] = isset($_COOKIE['time_personal']) ? $_COOKIE['time_personal'] : '';//缓存标示
        $result = My::getCache($search,array('pre'=>'personal'));
        $result['log'] = $_POST['log'] = My::decodeJson($result['log']);
        if($result['complete'] != '1')
            $log = $this->log($_POST,$result);
        else{
            $log = '完善了个人信息内容！';
            $_POST['complete'] = '2';
            $_SESSION['member']['complete'] = '2';//修改信息完整度
        }
        if($log == '')
            $this->ajaxReturn(201,'无数据进行修改');
        $log = array('date'=>'['.$date.']，'.'由“'.My::user('member.username').'”修改了个人信息内容。','detail'=>$log);
        $_POST['log']['personal'] = self::log2length($result['log']['personal'],$log,'2000');
        $_POST['log']['personal'][] = $log;
        $_POST['editdate'] = $date = date('Y-m-d H:i:s');
    	$ok = DB('homeuser')->clear()->where("id={$userid}")->save($_POST);
    	if($ok>=0){
            setcookie("time_personal",strtotime($date),null,'/');
            $this->ajaxReturn(200,"个人信息修改成功");	
        }
    	else		$this->ajaxReturn(300,"个人信息修改失败");
    }

    //打印页面数据
    public function print_infoAct(){
        $userid = My::arr2var($_SESSION,'member.memberid');
        $result = DB('homeuser')->clear()->where("id={$userid}")->field('password',true)->find();
        $result['params'] = My::decodeJson($result['params']);
        unset($id);
        $this->assign('result',$result);
        $this->display();
    }
   
    //教师个人信息页
    public function teacher_infoAct(){
        $params = My::arr2var($_POST,'params');
        if(!My::is_string($_POST['username'],array('c')))
            $this->ajaxReturn(300,'姓名填写有误！');
        if(!My::is_date($params['birthdate']))
            $this->ajaxReturn(300,'出生日期填写有误！');
        if(!My::is_string($params['nation'],array('c')))
            $this->ajaxReturn(300,'民族填写有误！');
        if(!My::is_string($params['member'],array('c')))
            $this->ajaxReturn(300,'党派填写有误！');
        if(!My::is_string($_POST['college_id']))
            $this->ajaxReturn(300,'请选择所属院系！');
        if(!My::is_string($params['degree'],array('c')))
            $this->ajaxReturn(300,'最后学历/学位填写有误！');
        if(!My::is_string($params['professional'],array('c')))
            $this->ajaxReturn(300,'职称填写有误！');
        if(!My::is_string($params['job'],array('c')))
            $this->ajaxReturn(300,'现任职务填写有误！');
        if(!My::is_tel($_POST['mobile']))
            $this->ajaxReturn(300,'手机号码填写有误！');
         if(!My::is_string($params['qq']))
            $this->ajaxReturn(300,'QQ填写有误！');
        if(!My::is_email($_POST['email']))
            $this->ajaxReturn(300,'邮箱填写有误！');
        if(!My::is_string($params['address']))
            $this->ajaxReturn(300,'联系地址及邮编填写有误！');
        if(isset($_POST['params']['teacher']))
            foreach($_POST['params']['teacher'] as $key => $value){
                foreach ($value as $keys => $values) {
                   $_POST['params']['teacher'][$keys][$key] = $values;
                   unset($_POST['params']['teacher'][$key]);
                }
            }
        $search['type'] = My::arr2var($_SESSION,'member.type');
        $search['time'] = isset($_COOKIE['time_personal_t']) ? $_COOKIE['time_personal_t'] : '';
        $result = My::getCache($search,array('pre'=>'personal'));
        if($result['complete'] != '1') //信息完整度，1-->不完整，2-->完整
            $log = $this->log($_POST,$result);
        else{
            $log = '完善了个人信息内容！';
            $_POST['complete'] = '2';
            $_SESSION['member']['complete'] = '2';//修改信息完整度
        }
        if($log == '')
            $this->ajaxReturn(201,'无数据进行修改');
        $result['log'] = $_POST['log'] = My::decodeJson($result['log']);
        $_POST['editdate'] = $date = date('Y-m-d H:i:s');
        $userid = My::arr2var($_SESSION,'member.memberid');
        $log = array('date'=>'['.$date.']，'.'由“'.My::user('member.username').'”修改了个人信息内容。','detail'=>$log);
        $_POST['log']['personal'] = self::log2length($result['log']['personal'],$log,'2000');
        $_POST['log']['personal'][] = $log;
        $ok = DB('homeuser')->clear()->where("id={$userid}")->save($_POST);
        if($ok>=0){
            setcookie("time_personal_t",strtotime($date),null,'/');
            $this->ajaxReturn(200,"个人信息修改成功");
        }
        else        $this->ajaxReturn(300,"个人信息修改失败");
    }

}